<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      title="计划选择"
      style="min-width: 1200px"
      draggable
      align-center
      destroy-on-close
      @close="closeDialog"
      class="select-mes-plan"
    >
      <div class="search-container">
        <el-form :model="searchParam" :inline="true">
          <el-form-item
            label="生产日期"
            style="width: 300px; margin-right: 20px"
          >
            <el-date-picker
              v-model="searchParam.planActualDateSearch"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
              @change="getplanActualDate"
            />
          </el-form-item>

          <el-form-item
            label="工序类型"
            style="width: 200px; margin-right: 20px"
          >
            <el-select
              v-model="searchParam.processesClassification"
              placeholder="请选择"
              clearable
              :disabled="props.processType != ''"
            >
              <el-option
                v-for="item in useDictionary('ProcessType')"
                :key="Number(item.value)"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品" style="margin-right: 20px">
            <!-- <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入"
            clearable
            @keyup.enter="search"
            readonly
            @click="showWorkOrderSelect"
          >
            <template #append>
              <el-button @click="resetMaterial"> 清除 </el-button>
            </template>
          </el-input> -->
            <el-input
              v-model="searchParam.materialName"
              placeholder="请输入"
              clearable
              @keyup.enter="search"
              :disabled="props.materialName != ''"
            >
            </el-input>
          </el-form-item>
          <el-form-item
            label="计划状态"
            style="width: 170px; margin-right: 20px"
          >
            <el-select
              v-model="searchParam.formStatus"
              placeholder="请选择"
              clearable
              :disabled="props.formStatus != ''"
            >
              <el-option
                v-for="item in useDictionary('ProduceOrderState')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item style="margin-right: 0px">
            <el-button type="primary" @click="search"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-container">
        <div class="formTable">
          <el-table
            ref="multipleTable"
            :data="tableData"
            style="width: 100%"
            max-height="calc(100vh - 200px)"
            highlight-current-row
            @current-change="handleCurrentChange1"
            border
          >
            <el-table-column
              label="计划编号"
              prop="processingOrderNumber"
              min-width="180"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              label="产品图号"
              prop="figureNumber"
              min-width="160"
              show-overflow-tooltip
            />
            <el-table-column
              label="产品名称"
              prop="materialName"
              min-width="180"
              show-overflow-tooltip
            >
              <template #default="scope">
                {{ scope.row.materialName }}
              </template>
            </el-table-column>
            <el-table-column
              label="生产批次号"
              prop="plannedBatch"
              align="center"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              label="生产日期"
              prop="planActualDate"
              min-width="100"
              show-overflow-tooltip
              align="center"
              :formatter="formatDate"
            />
            <el-table-column
              label="计划状态"
              prop="formStatus"
              min-width="100"
              align="center"
            >
              <template #default="scope">
                {{
                  useDictionary("ProduceOrderState", scope.row.formStatus ?? "")
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="隔离状态"
              prop="isolation"
              min-width="90"
              align="center"
            >
              <template #default="scope">
                <span v-if="scope.row.isolation === 0">未隔离</span>
                <span v-else-if="scope.row.isolation === 1">已隔离</span>
                <span v-else-if="scope.row.isolation === 2">修改时间</span>
                <span v-else-if="scope.row.isolation === 3">返修修改</span>
              </template>
            </el-table-column>
            <el-table-column
              label="计划数量"
              prop="planQuantity"
              min-width="100"
              show-overflow-tooltip
              header-align="center"
              align="right"
            />
            <el-table-column
              label="上工序合格数量"
              prop="upQuantity"
              min-width="130"
              show-overflow-tooltip
              header-align="center"
              align="right"
            />
            <el-table-column
              label="合格数量"
              prop="validQuantity"
              min-width="100"
              show-overflow-tooltip
              header-align="center"
              align="right"
            />
            <el-table-column
              label="返修数量"
              prop="invalidQuantity"
              min-width="100"
              show-overflow-tooltip
              header-align="center"
              align="right"
            />
            <el-table-column
              label="报废数量"
              prop="scrapQuantity"
              min-width="100"
              show-overflow-tooltip
              header-align="center"
              align="right"
            />
            <el-table-column
              label="计划开始时间"
              prop="planBeginTime"
              min-width="160"
              sortable
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="计划结束时间"
              prop="planEndTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="实际开始时间"
              prop="beginTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="实际结束时间"
              prop="endTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="创建时间"
              prop="createTime"
              min-width="160"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              label="创建人"
              prop="createdUserName"
              min-width="100"
              show-overflow-tooltip
              align="center"
            />
          </el-table>
          <el-pagination
            :background="true"
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :total="pageable.total"
            layout="total, sizes, prev, pager, next, jumper"
            class="mt-[20px]"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="loading" @click="handleSubmit"
            >确 定
          </el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import request from "@/utils/request";
defineOptions({
  name: "SelectMesPlan",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  brekList: {
    type: Object,
    default: () => {
      return {};
    },
  },
  materialCode: {
    type: String,
    default: () => {
      return "";
    },
  },
  materialId: {
    type: String,
    default: () => {
      return "";
    },
  },
  materialName: {
    type: String,
    default: () => {
      return "";
    },
  },
  processType: {
    type: String,
    default: () => {
      return "";
    },
  },
  formStatus: {
    type: String,
    default: () => {
      return "";
    },
  },
});

const multipleTable = ref();

function getplanActualDate(data: any) {
  if (data) {
    searchParam.value.planActualDateStart = data[0];
    searchParam.value.planActualDateEnd = data[1];
  } else {
    searchParam.value.planActualDateStart = "";
    searchParam.value.planActualDateEnd = "";
  }
}

//获取表格数据 api 方法
const requestApi = "/mes/mesplanworkingform/queryByPage";
//获取数据初始化参数
const initParam = {};
const searchParamInit = {
  materialId: "",
  materialCode: "",
  materialName: "",
  formStatus: "",
  shiftId: "",
  shiftSystemId: "",
  planActualDateSearch: [],
  planActualDateStart: "",
  planActualDateEnd: "",
  processesClassification: "",
};
// 初始化搜索参数数据
// 初始化搜索参数数据
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any, parentIndex = "") => {
  return data;
};

// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  async (newVal: boolean) => {
    visibleDialog.value = newVal;
    searchParam.value.materialCode = props.materialCode ?? "";
    searchParam.value.materialId = props.materialId ?? "";
    searchParam.value.materialName = props.materialName ?? "";
    searchParam.value.processesClassification = Number(props.processType) ?? "";
    searchParam.value.formStatus = props.formStatus ?? "";
    getTableList();
  }
);

// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  reset();
}
/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择物料");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const resetHandle = () => {
  Object.assign(searchParam.value, searchParamInit);
  searchParam.value.materialCode = props.materialCode ?? "";
  searchParam.value.materialName = props.materialName ?? "";
  searchParam.value.materialId = props.materialId ?? "";
  searchParam.value.processesClassification = Number(props.processType) ?? "";
  searchParam.value.formStatus = props.formStatus ?? "";
  getTableList();
};

//时间格式处理，yyyy-mm-dd
function formatDate(row, column) {
  const date = row[column.property];
  if (!date) return "";
  return date.split(" ")[0]; // 返回日期的年月日部分，而隐藏时间部分
}
</script>

<style scoped lang="scss">
:deep(.select-mes-plan .search-container) {
  padding: 0;
  box-shadow: none;
}
:deep(.el-table th .cell) {
  //   color: black;
  //   font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
